package com.neusoft.springboot05.dao;

import com.neusoft.springboot05.domain.T_item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Repository
public class TItemDAOImpl {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public T_item findItemByID(Integer id)
    {
        String sql = "select * from t_item where id=?";
        return jdbcTemplate.queryForObject(sql, new RowMapper<T_item>() {
            @Override
            public T_item mapRow(ResultSet resultSet, int i) throws SQLException {
                T_item item = new T_item();
                item.setId(resultSet.getInt("id"));
                item.setPname(resultSet.getString("pname"));
                item.setPrice(resultSet.getFloat("price"));
                item.setPnum(resultSet.getInt("pnum"));
                return item;
            }
        },new Object[]{id});
    }

    public List<Map<String,Object>> findItemsList()
    {
        String sql = "select * from t_item";
        return jdbcTemplate.queryForList(sql);
    }

    public List<T_item> findItemsList02()
    {
        String sql = "select * from t_item";
        return jdbcTemplate.query(sql, new RowMapper<T_item>() {
            @Override
            public T_item mapRow(ResultSet resultSet, int i) throws SQLException {
                T_item item = new T_item();
                item.setId(resultSet.getInt("id"));
                item.setPname(resultSet.getString("pname"));
                item.setPrice(resultSet.getFloat("price"));
                item.setPnum(resultSet.getInt("pnum"));
                return item;
            }
        });
    }
}
